<?php
	session_start();
	$message= "";
	if (isset($_POST['subject']) && isset($_POST['message']) && isset($_POST['email']) && isset($_POST['form_submitted'])){
		if($_POST['subject'] != "" && $_POST['message'] != "Please type your message here." && preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/", $_POST['email'])){
	
		//CAPTCHA checking
		//Go here for help editing captcha: http://recaptcha.net/plugins/php/
			require_once('includes/recaptchalib.php');
			require_once('config/recaptcha.config.inc');
			$resp = recaptcha_check_answer($privatekey,
										$_SERVER["REMOTE_ADDR"],
										$_POST["recaptcha_challenge_field"],
										$_POST["recaptcha_response_field"]);
			
			//implement better failure
			if (!$resp->is_valid) {
				$message.= "The reCAPTCHA wasn't entered correctly. Please go back and try it again.";
			}
		//END CAPTCHA checking
			else{
				require('includes/my_email.php'); //imports $emailAddress
				$to = $emailAddress;
				$subject = $_POST['subject'];
				$emailmessage = $_POST['message'];
				$from = "From: " . $_POST['email']; 
				$sent = mail($to, $subject, $emailmessage, $from);
				
				//this $message should be displayed lower.
				if ($sent) {
		            $message = "Your message was sent successfully.";
		        } 
		        else {
		            $message = "There was an error sending your message. Please try again.";
		        }
	        }
        }
	
	elseif($_POST['subject'] == ""){
		$message= "Please enter a subject for your message.";
	}
	elseif($_POST['message'] == "Please type your message here."){
		$message= "Please enter a message.";
	}
	elseif(!preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/", $_POST['email'])){
		$message= "Please enter a valid email address.";
	}
	}
	else{
		$message= "";
	}

	$page= "Locations_Contact";
	include('includes/header.php');
	include('includes/menubar.php');
?>
<div class="body_content">
<?php
	print"<p class=\"message\"><strong>$message</strong></p>";
?>
	<p class="center">We have three stores open 7 days a week at the following locations:</p>
	<div class="left_loc"><span class="underline">Brookfield:</span><br/>
	20 Federal Road<br/>
	Brookfield, CT 06804<br/>
	203-775-3032</div>
	<div class="right_loc"><span class="underline">Orange:</span><br/>
	464 Boston Post Road<br/>
	Orange, CT 06477<br/>
	203-795-0170</div>
	<div class="center_loc"><span class="underline">Newington:</span><br/>
	37 Ann Street<br/>
	Newington, CT 06111<br/>
	860-666-4400</div>
		

	<div id="map"><br/><br/><iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?ie=UTF8&amp;hl=en&amp;msa=0&amp;msid=114548164126965814033.0004851bb66af38d92483&amp;ll=41.450961,-73.097534&amp;spn=0.988137,1.757813&amp;z=9&amp;output=embed"></iframe><br /><span class="small"><a href="http://maps.google.com/maps/ms?ie=UTF8&amp;hl=en&amp;msa=0&amp;msid=114548164126965814033.0004851bb66af38d92483&amp;ll=41.450961,-73.097534&amp;spn=0.988137,1.757813&amp;z=9&amp;source=embed">View in a larger map</a></span></div>

<?php
//implement an email form and a CAPTCHA to prevent spamming. emails will go to customer service/sales department
//subject, body, return email address (handle if not logged in)
?>
	<p class="center" id="showform">Click Here to Contact DFS</p>
	<div id="contactusform">
	<form class="center" action="loc_contact.php" method="post">
		<fieldset>
			Subject:<br/><input type="text" size="30" name="subject" /> <br/><br/>
			Message:<br/><textarea rows="10" cols="28" name="message" ></textarea><br/><br/>
			Your Email:<br/><input type="text" value="<?php			
				if (isset($_SESSION['user'])){
					$email= $_SESSION['user'];
					
					//display the email address
					print("$email");				
				} 
				else {
					//else, display a text field for the user's email address to be entered
					print("");
				}?>" name="email" size="30"/><br/><br/>
			<div class="captcha_center">
			<?php
				//captcha
				//Go here for help editing captcha: http://recaptcha.net/plugins/php/
				require_once('includes/recaptchalib.php');
				require_once('config/recaptcha.config.inc');
				echo recaptcha_get_html($publickey);
			?></div>
			<input type="hidden" value="1" name="form_submitted" />
			<input type="submit" value="Send an email" name="message_submit" />
		</fieldset>
	</form>
	</div>
</div>
<?php
	include ('includes/footer.php');
?>
</body>
</html>